package com.easyrecite.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.easyrecite.dao.UserDao;
import com.easyrecite.activity.R;
import com.easyrecite.utils.PhoneCheckUtil;

import java.sql.SQLException;

public class SignupMainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText user_name;
    private EditText phone;
    private EditText password;
    private Button btn_signup;
    private ImageButton btn_back;
    private TextView phone_check;
    private TextView username_check;
    private TextView password_check;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        setContentView(R.layout.activity_signup);
        setHalfTransparent();
        init();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.signup_back:
                finish();
                break;
            case R.id.btn_signup:
                String Phone = phone.getText().toString();
                String Username = user_name.getText().toString();
                String Password = password.getText().toString();
                boolean flag1, flag2, flag3;
                if (Username.equals("")) {
                    flag1 = false;
                    username_check.setText("用户名格式错误");
                } else {
                    flag1 = true;
                    username_check.setText("");
                }
                if (!PhoneCheckUtil.isPhoneLegal(Phone) || Phone.equals("")) {
                    flag2 = false;
                    phone_check.setText("手机号格式错误");
                } else {
                    flag2 = true;
                    phone_check.setText("");
                }
                if (Password.equals("")) {
                    flag3 = false;
                    password_check.setText("密码格式错误");

                } else {
                    flag3 = true;
                    password_check.setText("");
                }
                if (flag1 && flag2 && flag3) {
                    UserDao userDao = new UserDao();
                    try {
                        if (android.os.Build.VERSION.SDK_INT > 9) {
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                        }
                        userDao.signUp(Username, Phone, Password);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    Intent intent = getIntent();
                    Bundle bundle = new Bundle();
                    bundle.putString("phone", Phone);
                    bundle.putString("password", Password);
                    intent.putExtras(bundle);
                    Toast.makeText(this, "注册成功!", Toast.LENGTH_LONG).show();
                    SignupMainActivity.this.setResult(100, intent);
                    SignupMainActivity.this.finish();
                }
                break;
        }
    }

    private void init() {
        user_name = findViewById(R.id.username);
        phone = findViewById(R.id.phone);
        phone_check = findViewById(R.id.phone_check);
        username_check = findViewById(R.id.username_check);
        password_check = findViewById(R.id.password_check);
        password = findViewById(R.id.password);
        btn_signup = findViewById(R.id.btn_signup);
        btn_signup.setOnClickListener(this);
        btn_back = findViewById(R.id.signup_back);
        btn_back.setOnClickListener(this);
        setHalfTransparent();
    }

    private void setHalfTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}